/////////////////////////////////////////////////////////////////////////////
// Original code from libhdfs3. Copyright (c) 2013 - 2014, Pivotal Inc.
// All rights reserved. Author: Zhanwei Wang
/////////////////////////////////////////////////////////////////////////////
//  Modifications by Kumo Inc.
// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//



#include <kmhdfs/common/exception.h>
#include <kmhdfs/common/exception_internal.h>
#include <kmhdfs/client/file_system_key.h>

#include <algorithm>
#include <libxml/uri.h>
#include <sstream>

namespace Hdfs {
namespace Internal {

FileSystemKey::FileSystemKey(const std::string & uri, const char * u) {
    xmlURIPtr uriobj;
    std::stringstream ss;
    ss.imbue(std::locale::classic());
    uriobj = xmlParseURI(uri.c_str());

    try {
        if (!uriobj || uriobj->server == NULL || 0 == strlen(uriobj->server)) {
            THROW(InvalidParameter,
                  "Invalid input: uri: %s is not a valid URI type.", uri.c_str());
        }

        host = uriobj->server;

        if (NULL == uriobj->scheme || 0 == strlen(uriobj->scheme)) {
            scheme = "hdfs";
        } else {
            scheme = uriobj->scheme;
        }

        if (strcasecmp(scheme.c_str(), "hdfs")) {
            THROW(InvalidParameter,
                  "Invalid input: uri is not a valid URI type.");
        }

        if (u && strlen(u) > 0) {
            user = UserInfo(u);
        } else if (NULL == uriobj->user || 0 == strlen(uriobj->user)) {
            user = UserInfo::LocalUser();
        } else {
            user = UserInfo(uriobj->user);
        }

        ss << user.getEffectiveUser();

        if (uriobj->port == 0) {
            ss << "@" << uriobj->server;
        } else {
            std::stringstream s;
            s.imbue(std::locale::classic());
            s << uriobj->port;
            port = s.str();
            ss << "@" << uriobj->server << ":" << uriobj->port;
        }

        authority = ss.str();
    } catch (...) {
        if (uriobj) {
            xmlFreeURI(uriobj);
        }

        throw;
    }

    xmlFreeURI(uriobj);
    std::transform(authority.begin(), authority.end(), authority.begin(), tolower);
    std::transform(scheme.begin(), scheme.end(), scheme.begin(), tolower);
}

}
}
